﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace IllustrateCsharp7_ch21_netframework
{
    class AsynchronizationOfSynchronousMethods
    {
        static void Main()
        {
            Console.WriteLine($"头部已执行，当前主线程Id为:{Thread.CurrentThread.ManagedThreadId}");
            string result = SayHiAsync("jack").Result;
            Console.WriteLine($"尾部已执行，当前主线程Id为:{Thread.CurrentThread.ManagedThreadId}");
            Console.ReadKey();
        }

        static Task<string> SayHiAsync(string name)
        {
            return Task.Run<string>(() => { return SayHi(name); });
        }

        static string SayHi(string name)
        {
            Task.Delay(2000).Wait();//异步等待2s
            Console.WriteLine($"sayHi执行，当前线程id为:{Thread.CurrentThread.ManagedThreadId}");
            return  $"Hello,{name}";
        }

    }
}
